// 内容反馈
function saveContentFeedback(){
    var params = $("#contentFeedback").find('form').serialize();
    $.ajax({
        url:api_path+'feedback/save',
        data:params,
        type:'post',
        dataType:'json',
        success:function(data){
            if (data.code == 2000) {
                cchUtil.message('提交成功，感谢您的反馈');
            }else{
                cchUtil.message(data.msg,'danger');
            }
        }
    });
}

// 对话滚动到底部
function scrollBottom(){
    var scrollHeight = $('#chatContent').parent().prop("scrollHeight");
    $('#chatContent').parent().scrollTop(scrollHeight,200);

    // 复制按钮
    var clipboard = new ClipboardJS('.copy_btn'); //先实例化
    clipboard.on('success', function(e) {
        cchUtil.message('复制成功');
        e.clearSelection();
    });
    clipboard.on('error', function(e) {
        cchUtil.message('复制失败','danger');
    });

    // 反馈按钮
    $(".feedback_btn").on('click',function(){
        var message_div = $(this).parents(".message");
        $("#contentFeedback").find('input[name="msg_id"]').val($(message_div).attr('id'));
        $("#contentFeedback").find('textarea[name="content"]').val($(message_div).find('.message-content span').html());
    });

    $(".avatar img").on('error',function(){
        $(this).attr('src',static_path+'/index/media/avatar/3.png');
    });
}

// 回车发送
$("#msg_text").on('keydown',function(event){
    if(event.keyCode==13){
       sendMsg();
       return false;
    }
});

// 生成对话HTML
function createMsgHtml(message,type,ctime,msg_id){
    if (message == null || message == '') {
      return '';
    }
    if (ctime == null || ctime == '') {
        ctime = getFamtterHours();
    }

    var dropdowns = '';
    if (type == 0) {
        var msg_class = 'message self';
        var avatar = upload_path+'/'+global_user.headimgurl;
    }else if(type == 1){
        var msg_class = 'message';
        var avatar = upload_path+'/'+global_assistant.image;
        if (msg_id != null) {
            dropdowns =  '<a class="dropdown-item d-flex align-items-center feedback_btn" href="javascript:void(0)" data-toggle="modal" data-target="#contentFeedback">'+
                            '<i class="mdi mdi-pencil hw-18 mr-2"></i>'+
                            '<span>反馈建议</span>'+
                        '</a>';
        }
    }else{
        var msg_class = 'message msg_loading';
        var avatar = upload_path+'/'+global_assistant.image;
    }

    var str = '<div class="'+msg_class+'" id="'+msg_id+'">'+
                '<div class="message-wrapper">'+
                    '<div class="message-content">'+
                        '<span>'+message+'</span>'+
                    '</div>'+
                '</div>'+
                '<div class="message-options">'+
                    '<div class="avatar avatar-sm"><img alt="" src="'+avatar+'"></div>'+
                    '<span class="message-date">'+ctime+'</span>'+
                    '<div class="dropdown">'+
                        '<a class="text-muted" href="#" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">'+
                            '<svg class="hw-18" fill="none" viewBox="0 0 24 24" stroke="currentColor">'+
                                '<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 12h.01M12 12h.01M19 12h.01M6 12a1 1 0 11-2 0 1 1 0 012 0zm7 0a1 1 0 11-2 0 1 1 0 012 0zm7 0a1 1 0 11-2 0 1 1 0 012 0z"/>'+
                            '</svg>'+
                        '</a>'+

                        '<div class="dropdown-menu">'+
                            '<a class="dropdown-item d-flex align-items-center copy_btn" href="javascript:void(0)" data-clipboard-text="'+message+'">'+
                                '<svg class="hw-18 mr-2" fill="none" viewBox="0 0 24 24" stroke="currentColor">'+
                                    '<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 16H6a2 2 0 01-2-2V6a2 2 0 012-2h8a2 2 0 012 2v2m-6 12h8a2 2 0 002-2v-8a2 2 0 00-2-2h-8a2 2 0 00-2 2v8a2 2 0 002 2z"/>'+
                                '</svg>'+
                                '<span>复制</span>'+
                            '</a>'+
                            dropdowns+
                        '</div>'+
                    '</div>'+
                '</div>'+
            '</div>';

    return str;
}